package com.example.springboot.service.Impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.common.ResultCode;
import com.example.springboot.common.config.JwtTokenUtils;
import com.example.springboot.dao.AdminMapper;
import com.example.springboot.dao.CommentMapper;
import com.example.springboot.dao.UserMapper;
import com.example.springboot.domain.po.Account;
import com.example.springboot.domain.po.Admin;
import com.example.springboot.domain.po.Comment;
import com.example.springboot.domain.po.User;
import com.example.springboot.exception.CustomException;
import com.example.springboot.service.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.List;

/**
 * @Author：伐柯梦姝
 * @Package：com.example.springboot.service.Impl
 * @Project：springboot
 * @name：CommentServiceImpl
 * @Date：2025/3/2 16:23
 * @Filename：CommentServiceImpl
 * @for:
 */


@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

       @Resource
       private CommentMapper commentMapper;

       @Resource
       private UserMapper userMapper;

       @Resource
       private AdminMapper adminMapper;

       @Override
       public Comment add(Comment comment) {
              comment.setTime(DateUtil.now());//设评论时间为当前时间
              commentMapper.insert(comment);
              return comment;
       }


       @Override
       public Comment findById(Integer id) {
              return commentMapper.selectById(id);
       }


       @Override
       public void delete(Integer id) {
              commentMapper.deleteById(id);
       }

       @Override
       public List<Comment> findAll(Integer newsId) {
              Account currentUser = JwtTokenUtils.getCurrentUser();
              if (ObjectUtil.isNull(currentUser)) {
                     throw new CustomException(ResultCode.LOGIN_CHECK_ERROR);
              }
              List<Comment> list = this.findByNewsIdAndCommentId(newsId, 0);
              for (Comment comment : list) {
                     setData(comment);
                     List<Comment> children = this.findByNewsIdAndCommentId(newsId, comment.getId());
                     if (CollectionUtil.isNotEmpty(children)) {
                            for (Comment child : children) {
                                   setData(child);
                            }
                            comment.setChildren(children);
                     }
              }
              return list;
       }

       @Override
       public void setData(Comment comment) {
              if (1 == comment.getRole()) {
                     Admin admin = adminMapper.selectById(comment.getUserId());
                     comment.setUsername(admin.getUsername());
                     comment.setAvatar(admin.getAvatar());
              }
              if (2 == comment.getRole()) {
                     User user = userMapper.selectById(comment.getUserId());
                     comment.setUsername(user.getUsername());
                     comment.setAvatar(user.getAvatar());
              }
       }

       @Override
       public PageInfo<Comment> findPage(Comment search, Integer pageNum, Integer pageSize) {
              Account user = JwtTokenUtils.getCurrentUser();
              if (ObjectUtil.isEmpty(user)) {
                     throw new CustomException(ResultCode.USER_NOT_LOGIN);
              }
              PageHelper.startPage(pageNum, pageSize);
              List<Comment> all = findByCondition(search,user);
              for (Comment comment : all) {
                     Integer role = comment.getRole();
                     if (1 == role){
                            Admin admin = adminMapper.selectById(comment.getUserId());
                            comment.setUsername(admin.getUsername());
                     }
                     if (2 == role){
                            User dbuser = userMapper.selectById(comment.getUserId());
                            comment.setUsername(dbuser.getUsername());
                     }

              }

              return PageInfo.of(all);
       }

       @Override
       public List<Comment> findByCondition(Comment search,Account user) {
              if (1 == user.getRole()){
                     return this.findBySearch(search,null,null);
              } else {
                     return this.findBySearch(search, user.getId(), user.getRole());
              }

       }



       public List<Comment> findByNewsIdAndCommentId(Integer newsId, Integer commentId){
              List<Comment> list = this.lambdaQuery()
                      .eq(ObjectUtil.isNotEmpty(newsId), Comment::getNewsId, newsId)
                      .eq(ObjectUtil.isNotEmpty(commentId), Comment::getCommentId, commentId)
                      .list();
              return list;
       }

       public List<Comment> findBySearch(Comment comment, Integer userId, Integer role){
              List<Comment> list = this.lambdaQuery()
                      .like(ObjectUtil.isNotEmpty(comment) && ObjectUtil.isNotEmpty(comment.getTitle()), Comment::getTitle, comment.getTitle())
                      .eq(ObjectUtil.isNotEmpty(userId), Comment::getUserId, userId)
                      .eq(ObjectUtil.isNotEmpty(role), Comment::getRole, role)
                      .like(ObjectUtil.isNotEmpty(comment.getUsername()), Comment::getUsername, comment.getUsername())
                      .list();
              return list;
       }


}